package so.shanku.essential.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.jpush.android.api.JPushInterface;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.json.JsonMap;
import net.tsz.afinal.json.JsonParseHelper;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseActivity;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.getdata.GetDataConfing;
import so.shanku.essential.getdata.GetDataQueue;
import so.shanku.essential.getdata.IGetServicesDataCallBack;
import so.shanku.essential.getdata.ShowGetDataError;
import so.shanku.essential.utils.ExtraKeys;
import so.shanku.essential.utils.Utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Jaelyn on 2015/8/17 0017.
 */
public class NoLoginActivity extends BaseActivity {
	/**
	 * 未登录的界面
	 */
	@ViewInject(id = R.id.option_layout)
	private LinearLayout option_layout;
	@ViewInject(id = R.id.login_bt, click = "login_bt_click")
	private Button login_bt;
	@ViewInject(id = R.id.register_bt, click = "register_bt_click")
	private Button register_bt;
	@ViewInject(id = R.id.third_login_tv, click = "third_login_tv_click")
	private TextView third_login_tv;
	@ViewInject(id = R.id.vistor_login_tv, click = "vistor_login_tv_click")
	private TextView vistor_login_tv;

	@ViewInject(id = R.id.aggressment_tv, click = "aggressment_tv_click")
	private TextView aggressment_tv;

	private int flag = 0;//0 app启动正常进入的   1：已经启动，再次进入登录

	private int requestLogin = 1;
	private int requestRegister = 2;
	private int interception_request = 3;
	private int thirdLogin=4;

	/**
	 * 登录界面
	 */
//	@ViewInject(id=R.id.login_layout)
//	private RelativeLayout login_layout;
//
//	@ViewInject(id=R.id.user_account_ed)
//	private EditText user_account_ed;
//
//	@ViewInject(id=R.id.user_pwd_ed)
//	private EditText user_pwd_ed;
//
//	@ViewInject(id=R.id.forget_pwd_tv)
//	private TextView forget_pwd_tv;
//
//	@ViewInject(id=R.id.login_login_bt,click = "login_login_bt_click")
//	private TextView login_login_bt;
//
//	//保存历史现实的界面
//	private List<View> views=new ArrayList<View>();
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_nologin_layout);
		flag = getIntent().getIntExtra(ExtraKeys.Key_Msg1, 0);
		if (flag == 1) {
			Intent intentLogin = new Intent(this, RegisterActivity2.class);
			intentLogin.putExtra(ExtraKeys.Key_Msg1, flag);
			startActivityForResult(intentLogin, interception_request);
			option_layout.setVisibility(View.INVISIBLE);
		} else {
			option_layout.setVisibility(View.VISIBLE);
			addUnderLine(third_login_tv);
			addUnderLine(aggressment_tv);
			addUnderLine(vistor_login_tv);
		}
	}


	/**
	 * user login
	 *
	 * @param view
	 */
	public void login_bt_click(View view) {
		Intent intentLogin = new Intent(this, LoginActivity.class);
		intentLogin.putExtra(ExtraKeys.Key_Msg1, flag);
		intentLogin.putExtra(ExtraKeys.Key_Msg2, 1);
		startActivityForResult(intentLogin, requestLogin);
		//jumpTo(LoginActivity.class);
	}

	/**
	 * user login
	 *
	 * @param view
	 */
	public void register_bt_click(View view) {
		//jumpTo(RegisterActivity.class);
		Intent intentLogin = new Intent(this, RegisterActivity.class);
		intentLogin.putExtra(ExtraKeys.Key_Msg1, flag);
		startActivityForResult(intentLogin, requestRegister);
	}

	/**
	 * 访客模式
	 *
	 * @param view
	 */
	public void vistor_login_tv_click(View view) {
		getData_Visiter();
	}

	/**
	 * 第三方登录
	 *
	 * @param view
	 */
	public void third_login_tv_click(View view) {
		Intent intentLogin = new Intent(this, LoginActivity.class);
		intentLogin.putExtra(ExtraKeys.Key_Msg1, flag);
		intentLogin.putExtra(ExtraKeys.Key_Msg2, 2);
		startActivityForResult(intentLogin, thirdLogin);
	}

	/**
	 * 用户协议
	 *
	 * @param view
	 */
	public void aggressment_tv_click(View view) {
		jumpTo(UrlWebviewActivity.class);
	}

	/**
	 * 登录界面的登录阿牛
	 *
	 * @param view
	 */
	public void login_login_bt_click(View view) {
		jumpToAndClearAll(MainActivity.class);
	}

	/**
	 * 访客登录接口
	 */
	public void getData_Visiter() {
		loadingToast.show();
		Map<String, Object> params = new HashMap<String, Object>();

		params.put("tag", JPushInterface.getRegistrationID(this));
		GetDataQueue queue = new GetDataQueue();
		queue.setCallBack(callBack);
		queue.setActionName(GetDataConfing.GetAction_RegisterTempUserInfo);
		queue.setWhat(GetDataConfing.what_RegisterTempUserInfo);
		queue.setParamsNoJson(params);
		getDataUtil.getData(queue);
	}

	/**
	 * 获取服务器数据的返回信息
	 */
	private IGetServicesDataCallBack callBack = new IGetServicesDataCallBack() {

		@Override
		public void onLoaded(GetDataQueue entity) {
			if (entity.isOk()) {
				JsonMap<String, Object> data = JsonParseHelper.getJsonMap(entity.getInfo());
				if (ShowGetDataError.isOkAndCodeIsNot1(mContext,
						entity.getInfo())) {
					if (entity.what == GetDataConfing.what_RegisterTempUserInfo) {
						List<JsonMap<String, Object>> info = data.getList_JsonMap("Info");
						JsonMap<String, Object> dataInfo = info.get(0);
						Utils.saveUserInfos(mContext, dataInfo);
						Utils.saveUserLoginStyle(mContext, true); //保存登录状态到本地，支付需要（true为访客登录，false为注册登录）
						MyApplication.getInstance().setAttentionBrands(null);
						MyApplication.getInstance().setPushMsgCount(0);
						MyApplication.getInstance().setShopcart_num(0);
						MyApplication.getInstance().setTempUserAccount(info.get(0).getStringNoNull("UserName"));
						Intent intentLogin = new Intent(mContext, MainActivity.class);
						intentLogin.putExtra(ExtraKeys.Key_Msg1, flag);
						jumpTo(intentLogin, true);
					}
				}
			} else {
				ShowGetDataError.showNetError(mContext);
			}
			loadingToast.dismiss();
		}
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == interception_request) {
			finish();
		} else {
			if (resultCode == RESULT_OK) {
				if (flag == 0) {//app启动进入
					if (requestCode==requestRegister||requestCode==thirdLogin&&MyApplication.getInstance().getAttentionBrands().size() == 0){
						Intent intentLogin = new Intent(this, MainActivity.class);
						intentLogin.putExtra(ExtraKeys.Key_Msg1, flag);
						jumpTo(intentLogin, true);
					} else {
						jumpTo(MainActivity.class, true);
					}
				} else {
					finish();
				}
			}

		}

	}

}